package cn.lrwjz.androidguide.helper;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import cn.lrwjz.androidguide.adapter.TouchHelperAdapter;

public class RecyclerItemTouchHelper extends ItemTouchHelper.Callback {
    private final TouchHelperAdapter adapter;
    private final boolean enableDrag;
    private final boolean enableSwipe;

    public RecyclerItemTouchHelper(TouchHelperAdapter adapter, boolean enableDrag, boolean enableSwipe) {
        this.adapter = adapter;
        this.enableDrag = enableDrag;
        this.enableSwipe = enableSwipe;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return enableDrag; // 启用长按拖动
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return enableSwipe;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int dragFlags = enableDrag ? ItemTouchHelper.UP | ItemTouchHelper.DOWN : 0;
        int swipeFlags = enableSwipe ? ItemTouchHelper.START | ItemTouchHelper.END : 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder source,
                         @NonNull RecyclerView.ViewHolder target) {
        if (enableDrag) {
            adapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
            return true;
        }
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        if (enableSwipe) {
            adapter.onItemDismiss(viewHolder.getAdapterPosition());
        }
    }
} 